package com.example.xyr.myapplication.ui

import android.content.Intent
import android.os.AsyncTask
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import cn.jpush.im.android.api.ContactManager
import cn.jpush.im.android.api.callback.GetUserInfoListCallback
import cn.jpush.im.android.api.model.UserInfo
import com.example.xyr.myapplication.R
import com.example.xyr.myapplication.adapter.ChatType
import com.example.xyr.myapplication.adapter.FriendAdapter
import com.example.xyr.myapplication.base.BaseActivity
import com.example.xyr.myapplication.model.BaseFriendBean
import com.example.xyr.myapplication.model.FriendMsgLite
import com.example.xyr.myapplication.myinterface.FOnItemClickListener
import com.example.xyr.myapplication.utils.*
import com.example.xyr.myapplication.view.SideBarView
import kotlinx.android.synthetic.main.activity_my_friend.*
import kotlinx.android.synthetic.main.titlebar_layout.*
import org.litepal.LitePal
import java.util.*


/**
 * Created by ylcf on 2018/8/29.
 */
class MyFrienActivity : BaseActivity(), FOnItemClickListener<BaseFriendBean>, View.OnClickListener {


    lateinit var adapter: FriendAdapter
    var list: ArrayList<BaseFriendBean> = arrayListOf()

    override fun getLayout(): Int = R.layout.activity_my_friend

    override fun init() {
        titleBar.setTitle("好友管理")
        adapter = FriendAdapter(this)
        friend_recycler.adapter = adapter
    }

    override fun initListener() {
        friend_recycler.layoutManager = LinearLayoutManager(this)
        adapter.setOnItemClickListener(this)
        titleBar.ivBack.setOnClickListener(this)
        //字母滑动监听
        friend_sidebar.setOnLetterSelectListen(object : SideBarView.LetterSelectListener {
            override fun onLetterSelected(letter: String?) {
                var position = adapter.getPositionForSelection(letter!![0])
                if (position != -1) {
                    friend_recycler.smoothScrollToPosition(position)
                }
            }

            override fun onLetterReleased(letter: String?) {
            }

            override fun onLetterChanged(letter: String?) {
            }

        })
    }

    override fun initDate(intent: MyIntent?) {
        var async = MyAsync()
        async.execute()
        //获取好友列表
        getFriend()
    }

    override fun onClick(positon: Int, t: BaseFriendBean) {
        LogUtil.e("tag","positon>>>>>>>"+positon)
        if (positon < 3) {
            when (positon) {
                0 -> this.startActivity(Intent(this, FindActivity::class.java))
            }
        }else {
            UserInfoActivity.initActivity(this,t)
        }
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> finish()
        }
    }

    fun getFriend() {
        ContactManager.getFriendList(object : GetUserInfoListCallback() {
            override fun gotResult(responseCode: Int, responseMessage: String, userInfoList: List<UserInfo>) {
                if (0 == responseCode) {
                    //获取好友列表成功
                    var userList = arrayListOf<BaseFriendBean>()
                    for (userInfo in userInfoList) {
                        var userFriend = BaseFriendBean()
                        userFriend.friendType = ChatType.FRIEND_TYPE
                        userFriend.piyin = PinYinUtils.getPinYin(userInfo.nickname)
                        userFriend.userInfo = userInfo
                        userList.add(userFriend)
                    }
                    // 数据在放在adapter之前需要排序
                    Collections.sort(userList, PinyinComparator())
                    adapter.addItems(userList)
                } else {
                    //获取好友列表失败
                }
            }
        })
    }


    inner class MyAsync : AsyncTask<Unit, Unit, List<BaseFriendBean>>() {
        override fun doInBackground(vararg params: Unit?): List<BaseFriendBean> {
            var lista: List<BaseFriendBean> = ResourceLink.setFriendData()
            val findAll: List<FriendMsgLite> = LitePal.findAll(FriendMsgLite::class.java)
            LogUtil.e("tag", "FriendMsgLite>>>>>>>>>>>" + findAll.toString())
            return lista
        }

        override fun onPostExecute(result: List<BaseFriendBean>?) {
            if (result != null) {
                list.addAll(result)
            }
            adapter.addData(list)
        }
    }

}